package com.ruoyi.system.outto.goods.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.V2.gameAttr.mapper.GameAttrTemplateMapper;
import com.ruoyi.system.V2.goodInfo.mapper.GoodsInfoImgMapper;
import com.ruoyi.system.V2.inter.mapper.ArtRecMapper;
import com.ruoyi.system.interAttr.attr.KKZHWAttr;
import com.ruoyi.system.interAttr.domain.InterAttr;
import com.ruoyi.system.interAttr.mapper.InterAttrMapper;
import com.ruoyi.system.model.domain.ArtRec;
import com.ruoyi.system.model.domain.GameAttrTemplate;
import com.ruoyi.system.model.domain.GoodsInfoImg;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.model.dto.GoodsInfoAndAttrValueDto;
import com.ruoyi.system.outto.goods.KKZHWApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class KKZHWApiImpl implements KKZHWApi {
    @Autowired
    private GoodsInfoImgMapper goodsInfoImgMapper;
    @Autowired
    InterAttrMapper interAttrMapper;
    @Autowired
    private GameAttrTemplateMapper gameAttrTemplateMapper;
    @Autowired
    KKZHWAttr kkzhwAttr;
    @Autowired
    private ArtRecMapper artRecMapper;

    /**
     * 组装添加游戏参数
     *
     * @param goodsInfoAndAttrValueDto 游戏信息
     * @param salePartner              合作商信息
     * @return
     */
    private Map<String, Object> params(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner, boolean isAdd) {
        Map<String, Object> params = new TreeMap<>();
        // 获取对应游戏信息
        String substring = salePartner.getSaleName().substring(salePartner.getSaleName().indexOf("_") + 1);
        InterAttr interAttr = interAttrMapper.selectInterAttrByConfig(substring, goodsInfoAndAttrValueDto.getGameId());
        if (ObjectUtil.isNotEmpty(interAttr)) {
            // 游戏名称
            params.put("categoryName", interAttr.getInterGameName());
            // 游戏 ID
            params.put("categoryId", Integer.valueOf(interAttr.getInterGameId()));
        } else {
            log.error("游戏未匹配{}", goodsInfoAndAttrValueDto.getGameName());
            throw new GlobalException("游戏匹配失败，当前游戏:" + goodsInfoAndAttrValueDto.getGameName());
        }

        // 主图
        params.put("pic", goodsInfoAndAttrValueDto.getTitleImg());
        //获取商品相关图片列表
        GoodsInfoImg goodsInfoImg = new GoodsInfoImg();
        goodsInfoImg.setGoodsId(goodsInfoAndAttrValueDto.getId());
        goodsInfoImg.setType(Constants.STR_1);
        // 游戏详情图片
        List<GoodsInfoImg> goodsInfoImgList = goodsInfoImgMapper.selectGoodsInfoImgList(goodsInfoImg);
        if (!goodsInfoImgList.isEmpty()) {
            StringBuilder goodsInfoImgsStr = new StringBuilder();
            for (GoodsInfoImg infoImg : goodsInfoImgList) {
                goodsInfoImgsStr.append("," + infoImg.getImgUrl());
            }
            // 内容图
            params.put("albumPics", goodsInfoImgsStr.substring(1));
        }
        if (!isAdd) {
            ArtRec artRecreq = new ArtRec();
            artRecreq.setConfig(salePartner.getId());
            artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
            List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
            ArtRec artRec = artRecs.get(0);
            // 商品编号
            params.put("productSn", artRec.getOutid());
        }
        // 商品价格
        params.put("price", goodsInfoAndAttrValueDto.getPrice());
        // ⼼理底价
        params.put("originalPrice", goodsInfoAndAttrValueDto.getPrice());
        // 是否⽀持议价 0不⽀持，1⽀持
        params.put("gameGoodsYijia", 0);
        // 是否⼀⼿账号 0⾮⼀⼿，1⼀⼿
        params.put("gameGoodsYishou", 0);
        // 联系电话
        params.put("gameCareinfoPhone", salePartner.getTwoOther());
        // ⽅便联系时间
        params.put("gameCareinfoTime", "0-23");
        // ⽤户账号，号主电话，⼀般与gameCareinfoPhone⼀致
        params.put("userPhone", salePartner.getTwoOther());
        // 账号描述
        params.put("description", goodsInfoAndAttrValueDto.getDes());
        // 带分类内容图
        params.put("albumPicsArrays", new JSONArray());

        //匹配属性
        Property(goodsInfoAndAttrValueDto, params, salePartner);

        // 商品游戏属性
        /*[{
            name:属性名称
            value:value均为 String类型，属性值有多个则⽤英⽂逗号分隔
        }]*/
//        params.put("extGameAttributeArrays", "");
        return params;
    }

    private Map<String, Object> Property(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, Map<String, Object> params, SalePartner salePartner) {
        //获取合作商名称
        String substring = salePartner.getSaleName();
        if (substring.contains("_")) {
            substring = substring.substring(salePartner.getSaleName().indexOf("_") + 1);
        }
        // 根据条件获取游戏属性对应信息
        InterAttr interAttr = interAttrMapper.selectInterAttrByConfig(substring, goodsInfoAndAttrValueDto.getGameId());
        // 开始匹配区服属性
        builderServer(goodsInfoAndAttrValueDto, params, interAttr.getServer());

        // 属性列表
        List<Map<String, Object>> attributesList = new ArrayList<>();
        // 开始匹配单选属性
        builderRadioAttr(goodsInfoAndAttrValueDto, attributesList, interAttr.getRadioAttr());
        //开始匹配多选属性
        builderCheckAttr(goodsInfoAndAttrValueDto, attributesList, interAttr.getCheckAttr());
        //开始匹配输入框属性
        builderInputAttr(goodsInfoAndAttrValueDto, attributesList, interAttr.getInputAttr());

        params.put("extGameAttributeArrays", attributesList);
        return params;
    }

    /**
     * 开始匹配区服属性
     *
     * @param goodsInfoAndAttrValueDto 商品信息
     * @param params                   参数
     * @param server                   服务匹配项
     */
    private void builderServer(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, Map<String, Object> params, String server) {
        // 开始匹配单选属性
        JSONObject jsonObject = JSONObject.parseObject(server);
        // 开始匹配一级区服
        JSONArray interServeReqs = jsonObject.getJSONArray("interServeReqs");
        // 获取服务器的完整名
        String allServersName = getAllServersName(goodsInfoAndAttrValueDto);
        // 遍历对照信息，获取对应服务名
        Map<String, String> resaultMap = new HashMap<>();
        recursionJsonArray(interServeReqs, "interServerRes", "YdServeName", allServersName, "", resaultMap);
        // 获取接口服务名
        String serversName = resaultMap.get(allServersName);
        if (StringUtils.isNotEmpty(serversName)) {
            // 区与服务器
            params.put("gameAccountQufu", serversName);
        } else {
            throw new GlobalException("游戏【" + goodsInfoAndAttrValueDto.getGameName() + "】：服务器匹配失败！");
        }
    }

    /**
     * 开始匹配单选属性
     *
     * @param goodsInfoAndAttrValueDto 商品信息
     * @param attributesList           参数
     * @param radioAttr                单选属性匹配项
     */
    private void builderRadioAttr(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, List<Map<String, Object>> attributesList, String radioAttr) {
        // 开始匹配单选属性
        JSONObject jsonObject = JSONObject.parseObject(radioAttr);
        // 获取游戏单选属性的子对象
        String interRadioReqs = jsonObject.getString("interRadioReqs");
        log.info(JSONObject.toJSONString(interRadioReqs));
        // 获取游戏所有对应单选属性信息的 Json 对象
        List<JSONObject> jsonObjects = JSONObject.parseArray(interRadioReqs, JSONObject.class);
        if (!org.springframework.util.CollectionUtils.isEmpty(jsonObjects)) {
            /**
             * 1、先查询自定义中所有单选的字段
             * 2、再查该账号单选字段中的选项
             * 3、再去接口属性匹配表过滤
             */
            GameAttrTemplate gameAttrTemplate = new GameAttrTemplate();
            gameAttrTemplate.setGameId(goodsInfoAndAttrValueDto.getGameId());
            gameAttrTemplate.setIsEnable("0");
            gameAttrTemplate.setFieldType("radio");
            // 通过指定条件获取所有游戏选项模板属性
            List<GameAttrTemplate> gameAttrTemplates = gameAttrTemplateMapper.selectGameAttrTemplateList(gameAttrTemplate);
            // 通过反射获取商品信息所有属性
            Field[] declaredFields = goodsInfoAndAttrValueDto.getClass().getDeclaredFields();

            List<JSONObject> defObjects = new ArrayList<>();
            // 将获取的所有内容放入到 list 对象中
            defObjects.addAll(jsonObjects);

            for (JSONObject object : jsonObjects) {
                for (GameAttrTemplate attrTemplate : gameAttrTemplates) {
                    String aavName = attrTemplate.getAavName();
                    for (Field declaredField : declaredFields) {
                        String name = declaredField.getName();
                        if (aavName.equals(name)) {
                            //一级属性
                            if (Objects.equals(object.getLong("YdConfigId"), attrTemplate.getId())) {
                                String InterConfigId = object.getString("InterConfigId");
                                defObjects.remove(object);//如果匹配上，则去掉
                                String interRadioReqs1 = object.getString("interRadioReqs");
                                List<JSONObject> jsonObjects2 = JSONObject.parseArray(interRadioReqs1, JSONObject.class);
                                declaredField.setAccessible(true);
                                try {
                                    //获取选择的值 例如  实名中的可二次
                                    Object o = declaredField.get(goodsInfoAndAttrValueDto);
                                    String oneValue = String.valueOf(o);
                                    if (StringUtils.isNotEmpty(oneValue)) {
                                        List<JSONObject> twoAttr = jsonObjects2.stream().filter(j -> Objects.equals(j.getString("YdConfigId"), oneValue)).collect(Collectors.toList());
                                        Boolean required = object.getBoolean("required");
                                        String interConfigName = object.getString("InterConfigName");
                                        String defaultValue = object.getString("defaultValue");
                                        if (org.springframework.util.CollectionUtils.isEmpty(twoAttr) && required) {
                                            if (StringUtils.isNotEmpty(defaultValue)) {
                                                Map<String, Object> attributesMap = new HashMap<>();
//                                            if (goodsInfoAndAttrValueDto.getGameName().equals("王者荣耀")) {
//
//                                            } else {
                                                attributesMap.put("name", InterConfigId);
                                                attributesMap.put("value", interConfigName);
                                                attributesList.add(attributesMap);
//                                            }
                                            }
                                        }
                                        if (org.springframework.util.CollectionUtils.isEmpty(twoAttr) && required && StringUtils.isEmpty(defaultValue)) {
                                            throw new GlobalException("属性" + interConfigName + " 接口要求必传，但是暂未匹配到，请设置默认值");
                                        }
                                        if (!org.springframework.util.CollectionUtils.isEmpty(twoAttr)) {
                                            Map<String, Object> attributesMap = new HashMap<>();
//                                        if (goodsInfoAndAttrValueDto.getGameName().equals("王者荣耀")) {
//
//                                        } else {
                                            attributesMap.put("name", InterConfigId);
                                            attributesMap.put("value", twoAttr.get(0).getString("InterConfigName"));
                                            attributesList.add(attributesMap);
//                                        }
                                        }
                                    } else {
                                        Map<String, Object> attributesMap = new HashMap<>();
//                                    if (goodsInfoAndAttrValueDto.getGameName().equals("王者荣耀")) {
//
//                                    } else {
                                        attributesMap.put("name", InterConfigId);
                                        attributesMap.put("value", jsonObjects2.get(0).getString("name"));
//                                    }
                                        attributesList.add(attributesMap);
                                    }
                                } catch (Exception e) {
                                    throw new GlobalException("单选项" + attrTemplate.getFieldName() + "匹配错误");
                                }
                            }
                        }
                    }
                }
            }
            //开始匹配默认值
            for (JSONObject object : defObjects) {
                String id = object.getString("id");
                String interRadioReqss = object.getString("interRadioReqs");
                List<JSONObject> jsonObjectss = JSONObject.parseArray(interRadioReqss, JSONObject.class);
                for (JSONObject jsonObject1 : jsonObjectss) {
                    if (StringUtils.isNotEmpty(jsonObject1.getString("defaultValue"))) {
                        Map<String, Object> attributesMap = new HashMap<>();
//                    if (goodsInfoAndAttrValueDto.getGameName().equals("王者荣耀")) {
//
//                    } else {
                        attributesMap.put("name", id);
                        attributesMap.put("value", jsonObject1.getString("name"));
                        attributesList.add(attributesMap);
//                    }
                        break;
                    }
                }
            }
        }
    }

    /**
     * 开始匹配多选属性
     *
     * @param goodsInfoAndAttrValueDto 商品属性
     * @param attributesList           参数
     * @param checkAttr                多选属性匹配项
     */
    private void builderCheckAttr(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, List<Map<String, Object>> attributesList, String checkAttr) {
        Field[] declaredFields = goodsInfoAndAttrValueDto.getClass().getDeclaredFields();
        JSONObject checkAttrJson = JSONObject.parseObject(checkAttr);
        String interCheckBoxReqs = checkAttrJson.getString("interCheckBoxReqs");
        List<JSONObject> checkJsonList = JSONObject.parseArray(interCheckBoxReqs, JSONObject.class);
        if (org.springframework.util.CollectionUtils.isEmpty(checkAttrJson)) {
            throw new GlobalException("接口多选属性暂未配置");
        }

        GameAttrTemplate gameAttrTemplate = new GameAttrTemplate();
        gameAttrTemplate.setGameId(goodsInfoAndAttrValueDto.getGameId());
        gameAttrTemplate.setIsEnable("0");
        List<String> types = new ArrayList<>();
        types.add("checkbox");
        types.add("boxradio");
        gameAttrTemplate.setFieldTypes(types);
        List<GameAttrTemplate> checkAttrTemplates = gameAttrTemplateMapper.selectGameAttrTemplateList(gameAttrTemplate);

        for (JSONObject object : checkJsonList) {
            for (GameAttrTemplate attrTemplate : checkAttrTemplates) {
                String aavName = attrTemplate.getAavName();
                for (Field declaredField : declaredFields) {
                    String name = declaredField.getName();
                    if (aavName.equals(name)) {
                        declaredField.setAccessible(true);

                        String ydConfigIdLeveOne = object.getString("YdConfigId");
                        if (StringUtils.isNotEmpty(ydConfigIdLeveOne)) {

                            String[] split2 = ydConfigIdLeveOne.split(",");
                            List<String> list2 = new ArrayList<>();
                            for (String s : split2) {
                                s = s.replace(" ", "");
                                list2.add(s);
                            }
                            if (list2.contains(attrTemplate.getId() + "")) {
                                Object o = null;
                                try {
                                    o = declaredField.get(goodsInfoAndAttrValueDto);
                                } catch (IllegalAccessException e) {
                                    throw new GlobalException("多选项" + attrTemplate.getFieldName() + "匹配错误");
                                }
                                String oneValue = String.valueOf(o);
                                //一级属性
                                String ydConfigId = object.getString("YdConfigId");
                                String InterConfigName = object.getString("InterConfigName");
                                String InterConfigId = object.getString("InterConfigId");
                                Boolean required = object.getBoolean("required");
                                if (StringUtils.isNotEmpty(ydConfigId)) {
                                    String[] split = ydConfigId.split(",");
                                    //一级属性已经匹配，开始匹配二级属性
                                    if (Arrays.asList(split).contains(attrTemplate.getId() + "")) {
                                        String interCheckBoxReqsStr = object.getString("interCheckBoxReqs");
                                        //开始匹配二级属性
                                        List<JSONObject> jsonObjects2 = JSONObject.parseArray(interCheckBoxReqsStr, JSONObject.class);
                                        /**
                                         * 咱们的f字段  多选项都是用空格 隔开，用,号切割数组，跟匹配的数据匹配、匹配上说明此账号勾选的有此属性
                                         */
                                        List<String> list = new ArrayList<>();
                                        for (JSONObject jsonObject1 : jsonObjects2) {
                                            if (StringUtils.isNotEmpty(oneValue)) {
                                                String[] split1 = oneValue.split(",");
                                                String ydConfigId1 = jsonObject1.getString("YdConfigId");
                                                for (String s : split1) {
                                                    if (Objects.equals(s, ydConfigId1)) {
                                                        String InterConfigId2 = jsonObject1.getString("InterConfigId");
                                                        list.add(InterConfigId2);
                                                    }
                                                }
                                            }

                                            if (StringUtils.isEmpty(oneValue) && required) {
                                                throw new GlobalException("1属性" + InterConfigName + "必传，但是暂未匹配");
                                            }
                                        }
                                        if (org.springframework.util.CollectionUtils.isEmpty(list) && required) {
                                            throw new GlobalException("2属性" + InterConfigName + "必传，但是暂未匹配");
                                        }
                                        if (StringUtils.isNotEmpty(list)) {
                                            Map<String, Object> attributesMap = new HashMap<>();
                                            attributesMap.put("name", InterConfigId);
                                            attributesMap.put("value", StringUtils.join(list, ","));
                                            attributesList.add(attributesMap);
                                        }
                                    }
                                } else {
                                    throw new GlobalException("属性参数错误，请匹配后重新配置");
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 开始匹配输入框属性
     *
     * @param goodsInfoAndAttrValueDto 商品属性
     * @param attributesList           参数
     * @param inputAttr                输入属性匹配项
     */
    private void builderInputAttr(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, List<Map<String, Object>> attributesList, String inputAttr) {
        Field[] declaredFields = goodsInfoAndAttrValueDto.getClass().getDeclaredFields();

        //========================开始匹配输入框=====================//
        JSONObject jsonObject1 = JSONObject.parseObject(inputAttr);
        String inputJson = jsonObject1.getString("inputJson");
        List<JSONObject> jsonObjects1 = JSONObject.parseArray(inputJson, JSONObject.class);
        GameAttrTemplate gameAttrTemplate = new GameAttrTemplate();
        gameAttrTemplate.setGameId(goodsInfoAndAttrValueDto.getGameId());
        gameAttrTemplate.setIsEnable("0");
        ArrayList types = new ArrayList<>();
        types.add("number");
        types.add("input");
        gameAttrTemplate.setFieldTypes(types);
        List<GameAttrTemplate> inputTemplates = gameAttrTemplateMapper.selectGameAttrTemplateList(gameAttrTemplate);
        List<JSONObject> defInputObjects = new ArrayList<>();
        defInputObjects.addAll(jsonObjects1);

        for (JSONObject object : jsonObjects1) {
            for (GameAttrTemplate inputTemplate : inputTemplates) {
                String aavName = inputTemplate.getAavName();
                if (Objects.equals(object.getString("aavName"), inputTemplate.getAavName())) {
                    defInputObjects.remove(object);
                    String aavNameInter = object.getString("interName");
                    String interId = object.getString("interId");
                    for (Field declaredField : declaredFields) {
                        String name = declaredField.getName();
                        if (Objects.equals(aavName, name)) {
                            declaredField.setAccessible(true);
                            String oneValue = null;
                            try {
                                oneValue = (String) declaredField.get(goodsInfoAndAttrValueDto);
                            } catch (IllegalAccessException e) {
                                throw new GlobalException("3属性参数错误，请匹配后重新配置");
                            }
                            Boolean required = object.getBoolean("required");
                            if (required && StringUtils.isEmpty(oneValue)) {
                                throw new GlobalException(aavNameInter + "必传，但是该商品没有该值");
                            }
                            if (StringUtils.isNotEmpty(oneValue)) {
                                Map<String, Object> attributesMap = new HashMap<>();
                                attributesMap.put("name", interId);
                                attributesMap.put("value", oneValue);
                                attributesList.add(attributesMap);
                            }
                        }
                    }
                }
            }
        }
        //开始匹配默认值
        for (JSONObject object : defInputObjects) {
            String id = object.getString("id");
            String defaultValue = object.getString("defaultValue");
            if (StringUtils.isNotEmpty(defaultValue)) {
                Map<String, Object> attributesMap = new HashMap<>();
                attributesMap.put("name", id);
                attributesMap.put("value", object.getString("name"));
                attributesList.add(attributesMap);
            }
        }
    }

    /**
     * 递归 json 对象
     *
     * @param objectJson     需要递归的 json 对象
     * @param childrenKey    级 key
     * @param returnValueKey 需要返回的值 value
     * @param allServersName yd 对应的服务名
     * @param parentServer   父级服务名
     * @param resultMap      返回的结果集
     */
    public void recursionJsonArray(Object objectJson, String childrenKey, String returnValueKey, String allServersName, String parentServer, Map<String, String> resultMap) {
        // 判断参数类型
        if (objectJson instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) objectJson;
            for (int i = 0; i < jsonArray.size(); i++) {
                recursionJsonArray(jsonArray.get(i), childrenKey, returnValueKey, allServersName, parentServer, resultMap);
            }
        } else if (objectJson instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) objectJson;
            Object object = jsonObject.get(childrenKey);
            // 判断是否为空
            if (ObjectUtil.isNotEmpty(object)) {
                recursionJsonArray(object, childrenKey, returnValueKey, allServersName, jsonObject.getString("name"), resultMap);
            } else {
                String value = jsonObject.getString(returnValueKey);
                if (StringUtils.isNotEmpty(value)) {
                    if (allServersName.contains(value)) {
                        resultMap.put(allServersName, (StringUtils.isNotEmpty(parentServer) ? parentServer + "|" : "") + jsonObject.getString("InterServeName"));
                    }
                }
            }
        }
    }

    /**
     * 获取服务器完整名
     *
     * @param goodsInfoAndAttrValueDto 商品信息
     * @return 服务器完整名
     */
    private String getAllServersName(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto) {
        // 获取所有的系统以及服务名
        StringBuffer allServerName = new StringBuffer();
        // 0：手游 1：端游
        if (StringUtils.equals(goodsInfoAndAttrValueDto.getGameType(), "0")) {
            // 系统名
            String systemName = goodsInfoAndAttrValueDto.getSystemName();
            if (StringUtils.isNotEmpty(systemName)) {
                allServerName.append(", ").append(systemName);
            }
            // 服务商名
            String carrierName = goodsInfoAndAttrValueDto.getCarrierName();
            if (StringUtils.isNotEmpty(carrierName)) {
                allServerName.append(", ").append(carrierName);
            }
            // 大区名称
            String groupName = goodsInfoAndAttrValueDto.getGroupName();
            if (StringUtils.isNotEmpty(groupName)) {
                allServerName.append(", ").append(groupName);
            }
        }
        // 服务器名
        String serverName = goodsInfoAndAttrValueDto.getServerName();
        if (StringUtils.isNotEmpty(serverName)) {
            allServerName.append(", ").append(serverName);
        }

        return allServerName.substring(2);
    }

    @Override
    public AjaxResult addGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner, boolean isAdd) {
        Map<String, Object> params = params(goodsInfoAndAttrValueDto, salePartner, isAdd);
        Map<String, Object> resultMap = new HashMap<>();
        log.info("看看账号添加请求参数={}", JSONObject.toJSONString(params));
        long timestamp = System.currentTimeMillis();
        String sign = kkzhwAttr.getSign(salePartner.getOther(), timestamp, "", JSONObject.toJSONString(params), false);
        try {
            //发送请求
            String response = HttpRequest.post("https://api2.kkzhw.com/mall-portal/openapi/import/product/add")
                    .header("X-KK-APIKEY", salePartner.getSaleKey())
                    .header("X-KK-TIME", String.valueOf(timestamp))
                    .header("X-KK-SIGNATURE", sign)
                    .body(JSONObject.toJSONString(params))
                    .execute()
                    .body();
            log.info("看看账号添加返回参数={}", response);
            JSONObject resultJson = JSONObject.parseObject(response);
            String data = resultJson.getString("data");
            if (!"200".equals(resultJson.getString("code"))) {
                resultMap.put("code", 500);
            } else {
                resultMap.put("code", 200);
                resultMap.put("outId", JSONObject.parseObject(data).getString("productSn"));
            }
            resultMap.put("msg", resultJson.getString("msg"));
            resultMap.put("req", JSONObject.toJSONString(params));
            resultMap.put("res", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success(resultMap);
    }

    @Override
    public AjaxResult editGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner, boolean isAdd) {
        return addGame(goodsInfoAndAttrValueDto, salePartner, isAdd);
    }

    @Override
    public AjaxResult delGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        Map<String, Object> resultMap = new HashMap<>();
        // 获取商品编号
        ArtRec artRecreq = new ArtRec();
        artRecreq.setConfig(salePartner.getId());
        artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
        List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
        ArtRec artRec = artRecs.get(0);
        Map<String, Object> params = new HashMap<>();
        params.put("productSn", artRec.getOutid());
        // 当前时间戳
        long timestamp = System.currentTimeMillis();
        // 获取头部参数
        String sign = kkzhwAttr.getSign(salePartner.getOther(), timestamp, "", JSONObject.toJSONString(params), false);
        log.info("看看账号删除商品请求参数：" + params);
        try {
            //发送请求
            String response = HttpRequest.post("https://api2.kkzhw.com/mall-portal/openapi/import/product/delete")
                    .header("X-KK-APIKEY", salePartner.getSaleKey())
                    .header("X-KK-TIME", String.valueOf(timestamp))
                    .header("X-KK-SIGNATURE", sign)
                    .body(JSONObject.toJSONString(params))
                    .execute()
                    .body();
            log.info("看看账号删除商品返回参数={}", response);
            JSONObject resultJson = JSONObject.parseObject(response);
            if (!"200".equals(resultJson.getString("code"))) {
                resultMap.put("code", 500);
            } else {
                resultMap.put("code", 200);
            }
            resultMap.put("req", JSONObject.toJSONString(params));
            resultMap.put("res", response);
            resultMap.put("msg", resultJson.getString("msg"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success(resultMap);
    }
}
